www.gusucode.com > VC++ 密码探测器 > VC++ 密码探测器/gusucode/PwdSpy/SingleInstance.cpp

    #include "stdafx.h"
#include "SingleInstance.h"

//***********************************************
CSingleInstance::CSingleInstance() : m_hMutex(NULL)
{
}

//***********************************************
CSingleInstance::~CSingleInstance()
{
	if(m_hMutex != NULL)
	{
		ReleaseMutex(m_hMutex);
		CloseHandle(m_hMutex);
	}
}

//***********************************************
bool CSingleInstance::Create(LPCTSTR szMutexName)
{
	_ASSERTE(szMutexName);
	_ASSERTE(lstrlen(szMutexName));

	bool bSuccess = false;

	try
	{
		// First get the handle to the mutex
		m_hMutex = CreateMutex(NULL, FALSE, szMutexName);
		if(m_hMutex != NULL)
		{
			// Test the state of the mutex
			// If the state is signaled, we successfully opened the mutex
			if(WaitForSingleObject(m_hMutex, 0) == WAIT_OBJECT_0)
				bSuccess = true;
		}
	}
	catch(...) {}

	return bSuccess;
}